/**
 * Created by yuanlian on 2016/3/24.
 */
angular.module('ng-datepicker',[])
    .directive('datePicker', [ function(){
        return {
            restrict: 'EC',
            require: '?ngModel',
            scope:{
                opens:'=opens',
                startDate:'=startDate',
                endDate:'=endDate',
                singleDatePicker:'=singleDatePicker'
            },
            replace: true,
            link: function (scope, element) {

                $(element).daterangepicker({
                    ranges:{
                        '今天': [moment(), moment()],
                        '昨天': [moment().subtract('days', 1), moment().subtract('days', 1)],
                        '过去七天': [moment().subtract('days', 6), moment()],
                        '过去30天': [moment().subtract('days', 29), moment()],
                        '本月': [moment().startOf('month'), moment().endOf('month')],
                        '上月': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
                    },
                    format:'YYYY-MM-DD',
                    singleDatePicker:scope.singleDatePicker||false,
                    startDate: scope.startDate||moment().subtract('days', 29),
                    endDate:scope.endDate||moment(),
                    minDate: "2016-01-01"
                }, function(start, end) {
                    scope.$apply(function () {
                        scope.startDate=start.format('YYYY-MM-DD');
                        scope.endDate=end.format('YYYY-MM-DD')
                    })

                });


            },
            template: '<input  class="form-control" type="text" placeholder="选择时间" >   '
        };
    }]);